import React from 'react';
import { Router, Route, Switch } from 'react-router-dom';
import history from '@/utils/history';
import Loading from './components/Loading';
import AuthRoute from './components/AuthRoute';

// 修改导入方式，使用React.lazy()
const Login = React.lazy(() => import('@/pages/Login'));
const Layout = React.lazy(() => import('@/pages/Layout'));
const NotFound = React.lazy(() => import('@/pages/NotFound'));
const Article = React.lazy(() => import('@/pages/Article'));
const Chat = React.lazy(() => import('@/pages/Profile/Chat'));
const Edit = React.lazy(() => import('@/pages/Profile/Edit'));
const Feedback = React.lazy(() => import('@/pages/Profile/Feedback'));
const Search = React.lazy(() => import('@/pages/Search'));
const SearchResult = React.lazy(() => import('@/pages/Search/Result'));

// 使用懒加载作用：提高浏览器加载速度
export default function App() {
  return (
    <Router history={history}>
      {/* 2.使用React.Suspense包住Switch组件 */}
      <React.Suspense fallback={<Loading />}>
        <Switch>
          {/* <Redirect exact from="/" to="/layout" /> */}
          <Route path="/login" component={Login} />
          <Route path="/search" component={Search} exact />
          <Route path="/search/result" component={SearchResult} />
          <Route path="/article/:id" component={Article} />
          <AuthRoute path="/profile/edit" component={Edit} />
          <AuthRoute path="/profile/chat" component={Chat} />
          <Route path="/" component={Layout} />
          <Route path="*" component={NotFound} />
        </Switch>
      </React.Suspense>
    </Router>
  );
}
